﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Enterprise.Framework.DomainModel;
using Enterprise.Framework.DomainModel.Repositories;
using Enterprise.Framework.Core.Exceptions;
using Resto.CostControl.Core.Domain.Resource;
using Resto.CostControl.Common.Utility;

namespace Resto.CostControl.Core.Domain.Model
{
    /// <summary>
    /// 订单
    /// </summary>
    public partial class DishOrder : IAggregationRoot
    {
        #region 构造函数

        internal DishOrder()
        {
            this.ID = IDHelper.CreateNewID();
        }

        #endregion


        #region 私有属性

        private IEntityRepository<DishOrder> _Repository;
        private IEntityRepository<DishOrder> Repository
        {
            get
            {
                if (_Repository == null)
                    _Repository = RestoCostDomainSession.CurrentSession.CreateEntityRepository<DishOrder>();

                return _Repository;
            }
        }

        #endregion

        #region 业务方法

        internal void Add()
        {
            this.CreateAudit = new Audit();
            this.LastUpdateAudit = new Audit();

            this.Repository.Add(this);
        }

        internal void Update()
        {
            this.LastUpdateAudit = new Audit();

            this.Repository.Update(this);
        }

        internal void Delete()
        {
            this.Repository.Delete(this);
        }

        internal void AddOrderItem(DishOrderItem item)
        {
            //判断菜单是否已经在列表中
            bool result = this.DishOrderItems.Where(p => p.DishName == item.DishName).Count() > 0;
            if (result)
                throw new BusinessValidationException(DishOrderResource.ValidateException_DishOrderItem_Repeat);

            this.DishOrderItems.Add(item);
        }

        internal void ClearItems()
        {
            DishOrderItem[] array = this.DishOrderItems.ToArray();
            foreach (DishOrderItem item in array)
                item.Delete();

            this.DishOrderItems.Clear();
        }

        /// <summary>
        /// 根据当前价格重新计算价格
        /// </summary>
        internal void ReComputePrice()
        {
            //todo:
        }

        #endregion
    }
}
